using MicroMsg.Manager;
using MicroMsg.Plugin.FaceBook;
using MicroMsg.Plugin.FaceBook.Scene;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
namespace MicroMsg.UI.Page
{
	public class FaceBookLoginPage : BasePage
	{
		private FaceBookLoginCallBack mFaceBookLoginCallBack;
		private bool isLogin = true;
		private LoadCompletedEventHandler loginHanlder;
		private bool isCancel;
		private int reTry;
		private string tokenCode = "";
		internal Grid LayoutRoot;
		internal WebBrowser wbLogin;
		internal Canvas LoadArea;
		internal PerformanceProgressBar progressBar;
		private bool _contentLoaded;
		public static void ShowFaceBookLoginPage(BasePage originPage, FaceBookLoginCallBack faceBookLoginCallBack_0, bool isFirstLoginout = true)
		{
			object[] array = new object[3];
			array[0] = faceBookLoginCallBack_0;
			array[1] = !isFirstLoginout;
			if (FaceBookMgr.GetIsFacebookAccountChanged())
			{
				array[1] = false;
				FaceBookMgr.SetIsFacebookAccountChanged(false);
			}
			ServiceCenter.statReportMgr.addStatItem(enKVStatKey.MM_FACEBOOK_CONNECT, "0");
			originPage.GoToPage("/Source/Plugin/Facebook/UI/FaceBookLoginPage.xaml", array, false);
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null && Enumerable.Count<object>(object_0) > 0)
			{
				this.mFaceBookLoginCallBack = (object_0[0] as FaceBookLoginCallBack);
				this.isLogin = (bool)object_0[1];
				return;
			}
		}
		protected override void OnPageOut()
		{
		}
		public FaceBookLoginPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.PhoneApplicationPage_Loaded));
			this.loginHanlder = new LoadCompletedEventHandler(this.wbLogin_LoadCompleted);
			this.wbLogin.add_LoadCompleted(this.loginHanlder);
		}
		private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
		{
			if (this.isLogin)
			{
				Uri loginUri = FBUris.GetLoginUri();
				this.wbLogin.Navigate(loginUri);
				return;
			}
			this.wbLogin.set_Visibility(1);
			Uri uri = new Uri("https://www.facebook.com/logout.php");
			this.wbLogin.Navigate(uri);
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			this.isCancel = true;
			if (this.mFaceBookLoginCallBack != null)
			{
				this.mFaceBookLoginCallBack(false, "");
			}
			base.OnBackKeyPress(cancelEventArgs_0);
		}
		private void wbLogin_LoadCompleted(object sender, NavigationEventArgs e)
		{
			if (this.isLogin)
			{
				this.wbLogin.set_Visibility(0);
				this.LoadArea.set_Visibility(1);
				string text = e.get_Uri().get_OriginalString().ToLower();
				if (text.StartsWith("http://www.facebook.com/connect/login_success.html?code="))
				{
					this.tokenCode = e.get_Uri().get_OriginalString().Substring(56);
					this.wbLogin.Navigate(FBUris.GetTokenLoadUri(this.tokenCode));
					ServiceCenter.statReportMgr.addStatItem(enKVStatKey.MM_FACEBOOK_LINK, "0");
					return;
				}
				if (text.StartsWith("https://graph.facebook.com/oauth/access_token"))
				{
					string strExpires = "";
					string accessToken = FaceBookLoginPage.getAccessToken(this.wbLogin.SaveToString(), ref strExpires);
					if (string.IsNullOrEmpty(accessToken))
					{
						if (this.reTry < 3 && !string.IsNullOrEmpty(this.tokenCode))
						{
							this.wbLogin.Navigate(FBUris.GetTokenLoadUri(e.get_Uri().get_OriginalString().Substring(56)));
							this.reTry++;
							return;
						}
						MMMessageBox.Show(strings.FaceBookNetInfo, true, "确定");
						ServiceCenter.statReportMgr.addStatItem(enKVStatKey.MM_FACEBOOK_LINK, "1");
						return;
					}
					else
					{
						if (this.isCancel)
						{
							return;
						}
						FaceBookMgr.UpdateTokenTime(strExpires);
						NetSceneFaceBookAuth netSceneFaceBookAuth = new NetSceneFaceBookAuth();
						netSceneFaceBookAuth.doScene(2, accessToken);
						base.GoBack();
						if (this.mFaceBookLoginCallBack != null)
						{
							this.mFaceBookLoginCallBack(true, accessToken);
							return;
						}
					}
				}
			}
			else
			{
				if (this.wbLogin.SaveToString().Contains("logout_form"))
				{
					this.wbLogin.InvokeScript("eval", new string[]
					{
						"document.forms['logout_form'].submit();"
					});
					return;
				}
				this.isLogin = true;
				Uri loginUri = FBUris.GetLoginUri();
				this.wbLogin.Navigate(loginUri);
			}
		}
		public static string getAccessToken(string inputStr, ref string strexpires)
		{
			string result = null;
			if (inputStr.Contains("access_token"))
			{
				int num = inputStr.IndexOf("access_token");
				string text = inputStr.Substring(num + 13);
				num = text.IndexOf("</pre>");
				text = text.Substring(0, num);
				result = text;
				num = text.IndexOf("&amp;expires=");
				if (num != -1)
				{
					int num2 = num + "&amp;expires=".get_Length();
					strexpires = text.Substring(num2, text.get_Length() - num2);
					text = text.Substring(0, num);
					result = text;
				}
			}
			return result;
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Facebook/UI/FaceBookLoginPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.wbLogin = (WebBrowser)base.FindName("wbLogin");
			this.LoadArea = (Canvas)base.FindName("LoadArea");
			this.progressBar = (PerformanceProgressBar)base.FindName("progressBar");
		}
	}
}
